// @Description
// @Author  xiaoyang
// @Date  2023/5/5 3:57 下午

package dto

import (
	"errors"
	"golang-manage-admin/lib"
	"golang-manage-admin/providers"
	"strconv"
	"time"
)

// DelayedHandleDataFormIn
/**
 * 延时操作：数据结构
 */
type DelayedHandleDataFormIn struct {
	Url  string `json:"url"`
	Body string `json:"body"`
}

// CheckData
/**
 *  @Author yang
 *  @Description
**/
func (dhdfi *DelayedHandleDataFormIn) CheckData() error {
	var errStr string
	if dhdfi.Url == "" {
		errStr += "Url：不是一个有效的URL； "
	}
	if len(errStr) != 0 {
		return errors.New("参数有误：" + errStr)
	}
	return nil
}

// DelayedHandelFormOut
/**
 *  @Author yang
 *  @Description 延时队列操作 -- Http发送数据
**/
type DelayedHandelFormOut struct {
	Ts   string `form:"_ts" json:"_ts" comment:"时间"`
	Rd   string `form:"_rd" json:"_rd" comment:"Key"`
	Sign string `form:"_sign" json:"_sign" comment:"签名"`
	Data string `form:"data" json:"data" comment:"内容体"`
}

// SetHandelFormData
/**
 *  @Author yang
 *  @Description 延时队列操作 -- Http发送数据 -- 构建发送体
**/
func (dhfo *DelayedHandelFormOut) SetHandelFormData(body string) DelayedHandelFormOut {
	ts := strconv.FormatInt(time.Now().Unix(), 10)
	rd := lib.GetRandomString(8)
	params := map[string]interface{}{
		"data": body,
		"_ts":  ts,
		"_rd":  rd,
	}
	sign := lib.ComGenSign(params, providers.Config.GetString("sign.rand_key"))
	return DelayedHandelFormOut{
		Ts:   ts,
		Rd:   rd,
		Sign: sign,
		Data: body,
	}
}
